//
//  PlayVideoViewController.m
//  CoolTennisBall
//
//  Created by CA on 16/4/1.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "PlayVideoViewController.h"
#import "KRVideoPlayerController.h"
#import "TrackVideo.h"
#import "RecordSportView.h"
#import "TrackRecordView.h"
#import "FlatUIKit.h"

@interface PlayVideoViewController ()
@property (nonatomic, strong) KRVideoPlayerController *videoController;
@property (weak, nonatomic) IBOutlet UIView *sportDetailView;
@property (weak, nonatomic) IBOutlet TrackRecordView *speedTrackView;
@property (weak, nonatomic) IBOutlet TrackRecordView *strengthTrackView;
@property (weak, nonatomic) IBOutlet TrackRecordView *arcTrackView;

@end

@implementation PlayVideoViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureLocalization];
    [self preferredInterfaceOrientationForPresentation];
    [self configureArcViews];
    
    self.sportDetailView.hidden = (self.networkUrlPath != nil);
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (self.networkUrlPath) {
        [self playVideoWithURL:[NSURL URLWithString:self.networkUrlPath]];
        if (self.coachVideoName != nil) {
            self.videoController.videoControl.topTitleLabel.text = self.coachVideoName;
        } else {
            self.videoController.videoControl.topTitleLabel.text = NSLocalizedString(@"Demo video", nil);
        }
        
    } else {
        NSURL *videoUrl = [NSURL fileURLWithPath:self.video.documentPath];
        [self playVideoWithURL:videoUrl];
        self.videoController.videoControl.topTitleLabel.text = self.video.name;
    }
}

#pragma mark - Localization

- (void)configureLocalization
{
    self.speedTrackView.nameLabel.text = NSLocalizedString(@"Speed", nil);
    self.strengthTrackView.nameLabel.text = NSLocalizedString(@"Strength", nil);
    self.arcTrackView.nameLabel.text = NSLocalizedString(@"Radian", nil);
}

#pragma mark - Private Method

// 播放视频
- (void)playVideoWithURL:(NSURL *)url
{
    if (!self.videoController) {
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        self.videoController = [[KRVideoPlayerController alloc] initWithFrame:CGRectMake(0, 0, width, width*(9.0/16.0))];
        __weak typeof(self)weakSelf = self;
        [self.videoController setDimissCompleteBlock:^{
            weakSelf.videoController = nil;
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }];
        
        self.videoController.refreshBlock = ^(NSTimeInterval currentTime) {
            
            if (weakSelf.networkUrlPath == nil) {
                NSPredicate *pred = [NSPredicate predicateWithFormat:@"swingSecond = %d",
                                     (int)currentTime];
                RLMResults *videoTrackItems = [weakSelf.video.videoTrackItems objectsWithPredicate:pred];
                if ([videoTrackItems count] > 0) {
                    VideoTrackItem *trackItem = [videoTrackItems lastObject];
                    [weakSelf configureForRecordSportViewWithItem:trackItem];
                }
                NSLog(@"搜索到的挥拍:%@", videoTrackItems);
            }
            
        };
        
        [self.view insertSubview:self.videoController.view atIndex:0];
        [self.videoController.videoControl removeFromSuperview];
        [self.view addSubview:self.videoController.videoControl];
        [self configuerVideoControllerUI];
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
        
    }
    self.videoController.contentURL = url;
}
- (void)configuerVideoControllerUI {
    UIImage *trackImage = [UIImage imageNamed:@"Train_icon_playSlider"];
    [self.videoController.videoControl.progressSlider setThumbImage:trackImage forState:UIControlStateNormal];
}


- (void)configureArcViews
{
    self.speedTrackView.arcView.total = @200;
    self.speedTrackView.arcView.duration = 1;
    self.speedTrackView.arcView.lineWidth = @6;
    self.speedTrackView.arcView.circleBackgroundColor = [UIColor colorWithWhite:1 alpha:0.1];
    self.speedTrackView.arcView.countingLabel.textColor = [UIColor whiteColor];
    self.speedTrackView.arcView.strokeColor = [UIColor colorWithHex:0x79B9EB];
    self.speedTrackView.arcView.countingLabel.font = [UIFont boldSystemFontOfSize:21.0f];
    [self.speedTrackView.arcView strokeChart];
    
    self.strengthTrackView.arcView.total = @20;
    self.strengthTrackView.arcView.duration = 1;
    self.strengthTrackView.arcView.lineWidth = @6;
    self.strengthTrackView.arcView.countingLabel.textColor = [UIColor whiteColor];
    self.strengthTrackView.arcView.circleBackgroundColor = [UIColor colorWithWhite:1 alpha:0.1];
    self.strengthTrackView.arcView.strokeColor = [UIColor colorWithHex:0xF06FA5];
    self.strengthTrackView.arcView.countingLabel.font = [UIFont boldSystemFontOfSize:21.0f];
    [self.strengthTrackView.arcView strokeChart];
    
    self.arcTrackView.arcView.total = @360;
    self.arcTrackView.arcView.duration = 1;
    self.arcTrackView.arcView.lineWidth = @6;
    self.arcTrackView.arcView.countingLabel.textColor = [UIColor whiteColor];
    self.arcTrackView.arcView.circleBackgroundColor = [UIColor colorWithWhite:1 alpha:0.1];
    self.arcTrackView.arcView.strokeColor = [UIColor colorWithHex:0xF8C353];
    self.arcTrackView.arcView.countingLabel.font = [UIFont boldSystemFontOfSize:21.0f];
    [self.arcTrackView.arcView strokeChart];
}


- (void)configureForRecordSportViewWithItem:(VideoTrackItem *)item
{
    [self.speedTrackView.arcView updateChartByCurrent:@(item.speed)];
    [self.strengthTrackView.arcView updateChartByCurrent:@((NSInteger)round(item.strength * 11.0/100))];
    [self.arcTrackView.arcView updateChartByCurrent:@(item.arc)];
}


#pragma mark - Rotate

//屏幕横屏
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

//限制屏幕横屏和录制视频横屏
-(BOOL)shouldAutorotate{
    return NO;
}


@end
